package org.example;

import java.util.*;

public class Test1 {
    //牛客 最小的k个数
    //https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=196&tqId=37099&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page&difficulty=undefined&judgeStatus=undefined&tags=590&title=
    public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
        ArrayList<Integer> ret = new ArrayList<>();
        if (input == null || input.length == 0 || k <= 0 || k > input.length) return ret;

        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
        for (int i = 0; i < k; i++) {
            queue.add(input[i]);
        }
        for (int i = k; i < input.length; i++) {
            if (input[i] < queue.peek()) {
                queue.poll();
                queue.add(input[i]);
            }
        }
        ret.addAll(queue);
        ret.sort(Comparator.naturalOrder()); // 确保结果升序
        return ret;
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        int[] arr = {0,1,2,1,2};
        List<Integer> ret = test1.GetLeastNumbers_Solution(arr,3);
        System.out.println(777);
    }
}
